package com.zhxl.server.controller;

import java.util.ArrayList;
import java.util.List;

import com.zhxl.server.dao.GoodsOrderItemDao;
import com.zhxl.server.model.GoodsOrderItem;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.zhxl.server.page.table.PageTableRequest;
import com.zhxl.server.page.table.PageTableHandler;
import com.zhxl.server.page.table.PageTableResponse;
import com.zhxl.server.page.table.PageTableHandler.CountHandler;
import com.zhxl.server.page.table.PageTableHandler.ListHandler;
import com.zhxl.server.dao.GoodsOrderDao;
import com.zhxl.server.model.GoodsOrder;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/goodsOrders")
public class GoodsOrderController {

    @Autowired
    private GoodsOrderDao goodsOrderDao;

    @Autowired
    private GoodsOrderItemDao goodsOrderItemDao;

    @PostMapping
    @ApiOperation(value = "保存")
    public GoodsOrder save(@RequestBody GoodsOrder goodsOrder) {
        goodsOrderDao.save(goodsOrder);

        return goodsOrder;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public GoodsOrder get(@PathVariable Long id,Model model) {
        GoodsOrder goodsOrder = goodsOrderDao.getById(id);
        return  goodsOrder;
    }

    @GetMapping("/test/{id}")
    @ApiOperation(value = "根据id获取")
    public List<GoodsOrderItem> getList(@PathVariable Long id,Model model) {
        List<GoodsOrderItem> list = goodsOrderItemDao.getByOrderId(id);
        return  list;
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public GoodsOrder update(@RequestBody GoodsOrder goodsOrder, Model model) {
        goodsOrderDao.update(goodsOrder);
        return goodsOrder;
    }

//    @ModelAttribute
//    public List<GoodsOrderItem> getById(PageTableRequest request){
//        List<GoodsOrderItem> list = new ArrayList<>();
//        list =  goodsOrderItemDao.list(request.getParams(), request.getOffset(), request.getLimit());
//        model.addAttribute("goods",list);
//        return list;
//    }
    @GetMapping
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return goodsOrderDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<GoodsOrder> list(PageTableRequest request) {
                return goodsOrderDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {
        goodsOrderDao.delete(id);
    }
}
